package com.aizu.component;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.sword.wechat4j.WechatSupport;

import com.aizu.entity.User;
import com.aizu.service.ERentService;
import com.aizu.service.RenterService;
import com.isp.wxchat.model.MsgType;

@Component("wxResponseHandler")
@Scope("prototype")
public class WxResponseHandler extends WechatSupport {

	private static Logger log = Logger.getLogger(WxResponseHandler.class);

	@Autowired
	private RenterService renterService;
	@Autowired
	private ERentService eRentService;

	@Value("${aizu.basePath}")
	public String basePath;

	@Override
	protected void onText() {
		// 文本测试
		if ("master_login".equals(wechatRequest.getContent()) || "renter_login".equals(wechatRequest.getContent())) {
			// responseText("你好" +
			// ("master_login".equals(wechatRequest.getContent()) ? "房主" : "房客")
			// + "，请确认<a href=\""
			// + basePath + "mvc/" + wechatRequest.getContent().replace('_',
			// '/') + ".htm?fromUserName="
			// + wechatRequest.getFromUserName() + "\">登陆</a>");
			responseNew("master_login".equals(wechatRequest.getContent()) ? "房源管理" : "房客租房", "马上进入", basePath
					+ "aizu/images/weixin/communityList.jpg",
					basePath + "mvc/" + wechatRequest.getContent().replace('_', '/') + ".htm?fromUserName="
							+ wechatRequest.getFromUserName());
		} else if ("master_order".equals(wechatRequest.getContent())
				|| "renter_order".equals(wechatRequest.getContent())) {
			responseText("你好" + ("master_order".equals(wechatRequest.getContent()) ? "房主" : "房客") + "，打开<a href=\""
					+ basePath + "mvc/" + wechatRequest.getContent().replace('_', '/') + "List" + ".htm?fromUserName="
					+ wechatRequest.getFromUserName() + "\">订单页面</a>");
		} else if (wechatRequest.getContent().equals(MsgType.news)) {
			responseNew(
					"图文消息",
					"测试图文消息",
					"http://upload.qqfuzhi.com/portal/showimg.php?img=e2dnYyk8PHEhIys9Y3t8Z3w9YGd8YXY9YmI9cHx%2BPHtnZ2NMen50f3xydz1wdHosPGFmYX8nTHEuJ3Z2IXFyJnUiICAqcnAnInYhcHJ2InAnKndycidwKyAgdnIqdiN1KitxdyojIysjcSAiJipyK3YqIXd1JCt1JyBxKnIkcCt1JyYkKysicCAjIiokKyogcHd1ICAhcXArciUjI3EhdyYjKiIncSclIiUqJyAkInEgKiV2IiEiJnEgKyp2cXV3cCEmJ3EjcHYqJHIrdytwIyYgIHIicHAgcXFwIiIldyIhNXIuISMrNXEuISMr",
					"http://www.chengn.com");

			// responseNew(title, description, picUrl, url);
			//
			// ArticleResponse item = new ArticleResponse();
			// item.setTitle(title);
			// item.setDescription(description);
			// item.setUrl(url);
			// item.setPicUrl(picUrl);
			// responseNews(item);
			//
			// List<ArticleResponse> items = new ArrayList<ArticleResponse>();
			// items.add(item);
			// responseNews(items);
		} else {
			// responseText("你好，你的输入为 " + wechatRequest.getContent() + "\n"
			// + "请按照如下操作输入:\n" + "1 文本\n" + "2 图文\n");
			responseText("很抱歉，暂时不提供回复服务。");
		}
	}

	/**
	 * 图片消息
	 */
	@Override
	protected void onImage() {
		String picUrl = wechatRequest.getPicUrl();
		String MediaId = wechatRequest.getMediaId();
		String MsgId = wechatRequest.getMsgId();

		String result = "图片消息picUrl:" + picUrl + ", MediaId:" + MediaId + ", MsgId:" + MsgId;
		log.info(result);
		responseText(result);
		// responseImage(mediaId);
	}

	/**
	 * 语音消息
	 */
	@Override
	protected void onVoice() {
		String Format = wechatRequest.getFormat();
		String MediaId = wechatRequest.getMediaId();// 视频消息媒体id，可以调用多媒体文件下载接口拉取数据
		String MsgId = wechatRequest.getMsgId();

		String result = "语音消息Format:" + Format + ", MediaId:" + MediaId + ", MsgId:" + MsgId;
		log.info(result);
		responseText(result);
		// responseVoice(mediaId);

		// 回复音乐消息
		// MusicResponse music = new MusicResponse();
		// music.setTitle(title);
		// music.setDescription(description);
		// music.setMusicURL(musicURL);
		// music.setHQMusicUrl(hQMusicUrl);
		// music.setThumbMediaId(thumbMediaId);
		// responseMusic(music);
		//
		// responseMusic(title, description, musicURL, hQMusicUrl,
		// thumbMediaId);
	}

	/**
	 * 视频消息
	 */
	@Override
	protected void onVideo() {
		String ThumbMediaId = wechatRequest.getThumbMediaId();
		String MediaId = wechatRequest.getMediaId();// 语音消息媒体id，可以调用多媒体文件下载接口拉取数据
		String MsgId = wechatRequest.getMsgId();

		String result = "视频消息ThumbMediaId:" + ThumbMediaId + ", MediaId:" + MediaId + ", MsgId:" + MsgId;
		log.info(result);
		responseText(result);

		// 回复视频消息
		// VideoResponse video = new VideoResponse();
		// video.setTitle(title);
		// video.setDescription(description);
		// video.setMediaId(mediaId);
		// responseVideo(video);
		//
		// responseVideo(mediaId, title, description);
	}

	/**
	 * 地理位置消息
	 */
	@Override
	protected void onLocation() {
		String Location_X = wechatRequest.getLocation_X();
		String Location_Y = wechatRequest.getLocation_Y();
		String Scale = wechatRequest.getScale();
		String Label = wechatRequest.getLabel();
		String MsgId = wechatRequest.getMsgId();

		String result = "地理位置消息Location_X:" + Location_X + ", Location_Y:" + Location_Y + ", Scale:" + Scale
				+ ", Label:" + Label + ", MsgId:" + MsgId;
		log.info(result);
		responseText(result);
	}

	/**
	 * 链接消息
	 */
	@Override
	protected void onLink() {
		String Title = wechatRequest.getTitle();
		String Description = wechatRequest.getDescription();
		String Url = wechatRequest.getUrl();
		String MsgId = wechatRequest.getMsgId();

		String result = "链接消息Title:" + Title + ", Description:" + Description + ", Url:" + Url + ", MsgId:" + MsgId;
		log.info(result);
		responseText(result);
	}

	/**
	 * 未知消息类型，错误处理
	 */
	@Override
	protected void onUnknown() {
		String msgType = wechatRequest.getMsgType();

		String result = "未知消息msgType:" + msgType;
		log.info(result);
		responseText(result);

	}

	/**
	 * 扫描二维码事件
	 */
	@Override
	protected void scan() {
		String FromUserName = wechatRequest.getFromUserName();
		String Ticket = wechatRequest.getTicket();

		String result = "扫描二维码事件FromUserName:" + FromUserName + ", Ticket:" + Ticket;
		log.info(result);
		responseText(result);
	}

	/**
	 * 订阅事件
	 */
	@Override
	protected void subscribe() {
		// String FromUserName = wechatRequest.getFromUserName();
		// 用户未关注时扫描二维码事件,会多一个EventKey和Ticket节点
		String Ticket = wechatRequest.getTicket();

		// String result = "订阅事件FromUserName:" + FromUserName;
		String result = "您好，欢迎关注云i租公众号";
		if (StringUtils.isNotBlank(Ticket)) {
			// result = "扫描带场景值二维码事件FromUserName:" + FromUserName + ", Ticket:"
			// + Ticket;
		}
		log.info(result);
		responseText(result);
	}

	/**
	 * 取消订阅事件
	 */
	@Override
	protected void unSubscribe() {
		String FromUserName = wechatRequest.getFromUserName();

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("wx_userid", FromUserName);
		try {
			User renter = renterService.loadUser(data);
			if (null == renter)
				log.info("用户不存在");
			data.clear();
			data.put("grantId", renter.getUserId());
			data.put("wxNum", FromUserName);
			eRentService.wxKCancel(data);
		} catch (Exception e) {
			e.printStackTrace();
		}

		String result = "取消订阅事件FromUserName:" + FromUserName;
		log.info(result);
		responseText(result);
	}

	/**
	 * 点击菜单跳转链接时的事件推送
	 */
	@Override
	protected void view() {
		String link = super.wechatRequest.getEventKey();
		log.info("点击菜单跳转链接时的事件推送link:" + link);
		responseText("点击菜单跳转链接时的事件推送link:" + link);
	}

	/**
	 * 自定义菜单事件
	 */
	@Override
	protected void click() {
		String key = super.wechatRequest.getEventKey();
		log.info("自定义菜单事件eventKey:" + key);
		if ("master_login".equals(key) || "renter_login".equals(key) || "master_order".equals(key)
				|| "renter_order".equals(key)) {
			wechatRequest.setContent(key);
			this.onText();
		} else {
			responseText("自定义菜单事件eventKey:" + key);
		}
	}

	/**
	 * 上报地理位置事件
	 */
	@Override
	protected void location() {
		String Latitude = wechatRequest.getLatitude();
		String Longitude = wechatRequest.getLongitude();
		String Precision = wechatRequest.getPrecision();
		String result = "上报地理位置事件Latitude:" + Latitude + ", Longitude:" + Longitude + ", Precision:" + Precision;
		log.info(result);
		responseText(result);
	}

	/**
	 * 模板消息发送成功推送事件
	 */
	@Override
	protected void templateMsgCallback() {
		String MsgID = wechatRequest.getMsgId();
		String Status = wechatRequest.getStatus();
		String result = "模板消息发送成功推送事件MsgID:" + MsgID + ", Status:" + Status;
		log.info(result);
	}

	/**
	 * 弹出地理位置选择器的事件
	 */
	@Override
	protected void locationSelect() {
		String Location_X = wechatRequest.getSendLocationInfo().getLocation_X();
		String Location_Y = wechatRequest.getSendLocationInfo().getLocation_Y();
		String Scale = wechatRequest.getSendLocationInfo().getScale();
		String Label = wechatRequest.getSendLocationInfo().getLabel();
		String Poiname = wechatRequest.getSendLocationInfo().getPoiname();
		String result = "弹出地理位置选择器的事件Location_X:" + Location_X + ", Location_Y:" + Location_Y + ", Scale:" + Scale
				+ ", Label:" + Label + ", Poiname:" + Poiname;
		log.info(result);
		responseText(result);
	}

	/**
	 * 弹出拍照或者相册发图的事件
	 */
	@Override
	protected void picPhotoOrAlbum() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String PicMd5Sum = "";
		if (StringUtils.isNotBlank(Count) && !Count.equals("0")) {
			PicMd5Sum = wechatRequest.getSendPicsInfo().getItem().get(0).getPicMd5Sum();
		}
		String result = "弹出系统拍照发图的事件Count:" + Count + ", PicMd5Sum:" + PicMd5Sum;
		log.info(result);
		responseText(result);
	}

	/**
	 * 弹出系统拍照发图的事件
	 */
	@Override
	protected void picSysPhoto() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String result = "弹出系统拍照发图的事件Count:" + Count;
		log.info(result);
		responseText(result);
	}

	/**
	 * 弹出微信相册发图器的事件推送
	 */
	@Override
	protected void picWeixin() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String result = "弹出系统拍照发图的事件Count:" + Count;
		log.info(result);
		responseText(result);
	}

	/**
	 * 扫码推事件
	 * 
	 */
	@Override
	protected void scanCodePush() {
		String ScanType = wechatRequest.getScanCodeInfo().getScanType();
		String ScanResult = wechatRequest.getScanCodeInfo().getScanResult();
		String result = "扫码推事件ScanType:" + ScanType + ", ScanResult:" + ScanResult;
		log.info(result);
		responseText(result);
	}

	/**
	 * 扫码推事件且弹出“消息接收中”提示框的事件
	 */
	@Override
	protected void scanCodeWaitMsg() {
		String ScanType = wechatRequest.getScanCodeInfo().getScanType();
		String ScanResult = wechatRequest.getScanCodeInfo().getScanResult();
		String result = "扫码推事件ScanType:" + ScanType + ", ScanResult:" + ScanResult;
		log.info(result);
		responseText(result);
	}

	@Override
	protected void onShortVideo() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void kfCreateSession() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void kfCloseSession() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void kfSwitchSession() {
		// TODO Auto-generated method stub

	}

}
